(function( $, undefined ) {
/*
	\name ru Ария текста
	\description ru Большое текстовое поле
	\save true
*/
$.widget("ui.ajajform_textareaedit",$.ui.ajajform_widget, {
	options: {
		// \type cssclass
		cssclass: "afw-textareaedit",
		// \type text
		label: "",
		// \type text
		value: "",
		// \type html
		htmltemplate: '<div class="af-label"></div><div class="af-edit"><textarea/><div class="af-error"></div></div><div style="clear: both;"></div>',
		htmltemplate_disableable: '<div class="af-label"></div><div class="af-disable"></div><div class="af-edit"><textarea/><div class="af-error"></div></div><div style="clear: both;"></div>',
		// \type integer
		width: 400,
		// \type integer
		height: 200,
		// \type boolean
		wysiwyg: false,
		wysiwyg_toolbar: "external",
		disabled: false,
		disableable: false,
		readonly: false,
		tinymce_settings: {
				script_url : '/shared/tiny_mce/jquery.tinymce.js',
				content_css : "/site.css",
				relative_urls : false,
				convert_urls: false,
				file_browser_callback : "openFileBrowser",
				language: ajajform_global_options.language,
				extended_valid_elements : "noindex",
				plugins : "safari,style,table,advhr,advimage,advlink,inlinepopups,media,searchreplace,contextmenu,paste,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
				body_class: "bodytinymce",
				theme : "advanced",
		 		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect,forecolor,backcolor",
			 	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,image,cleanup,code",
			 	theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,media,advhr"/*|clearleft,clearright,clearboth"*/,
//			 	theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
				theme_advanced_blockformats : "p,div,h1,h2,h3,h4,h5,h6,blockquote,cite,code,samp",
//				theme_advanced_toolbar_location : "top",
				theme_advanced_toolbar_align : "left",
//				theme_advanced_statusbar_location : "bottom",
				theme_advanced_resizing : true,
			}
	},
	_create: function() {
		this.genericCreate();
		this.refreshLabel();
		this.refreshTextarea();
		this.refreshValue();
		if (this.options.wysiwyg) {
			var t=this;
			setTimeout(function() { t.refreshWysiwyg();},10);
		}
		this.refreshDisabled();
		this.refreshReadonly();
	},
	refreshWysiwyg: function() {
		if (this.options.wysiwyg) {
			if (!this.options.value) {
				this.options.value="<p></p>";
				this.refreshValue();
			}
			if (!this.options.tinymce_settings.theme_advanced_toolbar_location) this.options.tinymce_settings.theme_advanced_toolbar_location=this.options.wysiwyg_toolbar;
			$("textarea",this.element).tinymce(this.options.tinymce_settings);
		}
	},
	refreshTextarea: function() {
		var i=$("textarea",this.element);
		i.addClass("ui-corner-all af-edit");
		i.width(this.options.width);
		i.height(this.options.height);
//		i.bind("focus",function(e) { $(this).addClass("ui-state-highlight"); });
		if (this.options.validate) {
			i.bind("blur",this,function(e) {
				$(this).removeClass("ui-state-highlight");
				e.data.validate();
			});
		} else {
			i.bind("blur",function(e) { $(this).removeClass("ui-state-highlight"); });
		}
	},
	refreshValue: function() {
		$("textarea",this.element).prop("value",this.options.value);
	},
	refreshReadonly: function() {
		$("textarea",this.element).prop("readonly",this.options.readonly);
	},
	refreshDisabled: function() {
		$("textarea",this.element).prop("disabled",this.options.disabled);
		this.genericRefreshDisabled();
	},
	destroy: function() {
	},
	validate: function() {
		var vd=this.options.validate;
		var inp=$("textarea",this.element);
		if (!vd) return false;
		var r=vd(inp.val());
		if (r) inp.addClass("ui-state-error"); else inp.removeClass("ui-state-error");
		this.refreshError(r);
		return r?true:false;
	},
	postvalue: function(r) {
		r[this.options.name]=$("textarea",this.element).val();
	}
});

})( jQuery );
